
import { app, BrowserWindow, Menu, Tray } from "electron";
import path from 'path';
import { windowMap } from '../window';

// 托盘对象（一般一个应用只有一个）
let tray: Tray;
// 闪烁图片路径数组
let pathArray: string[] = [];
// 默认图片路径
let rootPath = path.join(__dirname, '');
if (process.env.NODE_ENV === 'development') {
    rootPath = path.join(__dirname,'bundled')
}
// 闪烁计时器
let interval: any = null;
// 闪烁下标
let flashingIndex = 0;

//绑定右键菜单
const trayMenu = Menu.buildFromTemplate([
  // {
  //   label:'设置',
  //   click:function(){
  //     console.log('setting')
  //   }
  // },
  {
      label:'显示',
      click:function(){
        endFlashing();
        const mainID : any = windowMap.get("main");
        const win: any = BrowserWindow.fromId(mainID);
        win.show();
      }
  },
  {
      label:'退出',
      click:function(){
          if (process.platform !== 'darwin') {
            app.exit();
          }
      }
  }
]);


/**
 * 启用托盘
 *
 * @export
 * @param {string} imgPath
 */
export function enableTray(imgPath: string) {
    app.whenReady().then(() => {
        if (tray) {
            console.log("托盘已经启用，不需要重新启用");
            return;
        }
        tray = new Tray(path.join(rootPath, imgPath));
        tray.setImage(path.join(rootPath, imgPath))
        console.log(imgPath);
        pathArray.push(path.join(rootPath, imgPath));
        if (tray) {
          tray.setContextMenu(trayMenu);
          //监听任务栏图标的点击事件
          tray.on('click',function(){
            endFlashing();
            const mainID : any = windowMap.get("main");
            const win: any = BrowserWindow.fromId(mainID);
            win.show();
          })
        }
    });
}


/**
 * 开始闪烁
 *
 * @export
 */
export function startFlashing(timer: number) {
    if (!tray) {
        console.log("闪烁功能需开启托盘");
        return;
    }
    if (pathArray.length <= 1) {
        console.log("闪烁图片数量小于2 无法切换");
        return;
    }
    if (interval) {
      clearInterval(interval);
    }
    interval = setInterval(() => {
        if (pathArray.length <= 1) {
            console.log("闪烁图片数量小于2 无法切换");
            return;
        }
        if (pathArray.length < flashingIndex + 1) {
            flashingIndex = 0;
        }
        tray.setImage(pathArray[flashingIndex]);
        flashingIndex++;
    }, timer);
}


/**
 * 停止闪烁
 *
 * @export
 * @param {number} timer
 * @return {*} 
 */
export function endFlashing() {
    if (interval) {
        clearInterval(interval);
        if(pathArray.length > 0){
            tray.setImage(pathArray[0]);
        }
    }
}


/**
 * 添加托盘闪烁图片
 *
 * @export
 * @param {string} _path
 * @param {number} timer
 * @return {*} 
 */
export function addFlashingPath(_path: string) {
    pathArray.push(path.join(rootPath, _path));
}


/**
 * 删除指定闪烁图片
 *
 * @export
 * @param {string} _path
 */
export function deleteFlashingPath(_path: string) {
    const index = pathArray.findIndex((item: any) => { _path === item });
    if (index !== -1) {
        delete pathArray[index];
    }
}


/**
 * 清空闪烁图片
 *
 * @export
 */
export function deleteAllFlashing() {
    //  保留托盘图标
    pathArray = [pathArray[0]];
    endFlashing();
}
